package com.tangcco.controller;

import com.tangcco.biz.CommentBiz;
import com.tangcco.pojo.Comment;
import com.tangcco.pojo.Page;
import com.tangcco.pojo.VoteLog;
import com.tangcco.utils.PageUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/comment")
public class CommentController {
	@Resource
	private CommentBiz commentBiz;

	//查询最新评论
	@RequestMapping("/selectNewestComment")
	public Object selectByMusicRid(String musicRid,Integer currentPage, Model model){
//		musicRid = "7149583";
//		String id = musicRid.substring(6,musicRid.length());
		Integer everyPage = 20;
		Integer totalCount = commentBiz.selectCommentCount(musicRid);
		Page page = PageUtil.createPage(everyPage,totalCount,currentPage);
		List<Comment> commentList = commentBiz.selectByMusicRid(musicRid,page);
		List<VoteLog> voteLogs = commentBiz.selectVoteLog();
		for (Comment comment : commentList) {
			if(comment.getIsReply() == 1){
				comment.setReply(commentBiz.selectOldComment(comment.getcId()));
			}
		}
//		if(commentList.size() > 0){
			model.addAttribute("newestCommentList",commentList);
			model.addAttribute("page",page);
			model.addAttribute("voteLogs",voteLogs);
//		}
		return "newestCommentList";
	}

	//查询热门评论
	@RequestMapping("/selectHotComment")
	public Object selectHotComment(String musicRid,Model model){
//		String id = musicRid.substring(6,musicRid.length());
		List<Comment> hotCommentList = commentBiz.selectHotComment(musicRid);
		List<VoteLog> voteLogs = commentBiz.selectVoteLog();
//		if(hotCommentList.size() > 0){
			model.addAttribute("hotCommentList",hotCommentList);
			model.addAttribute("voteLogs",voteLogs);
			return "hotCommentList";
//		}
//		return null;
	}

	//添加评论
	@ResponseBody
	@RequestMapping("/insertComment")
	public Object insertComment(Integer cId,String uName,String msg,Integer uId,String mId,Integer isReply) {
		Date createTime = new Date();
		Map<String,String> map = new HashMap<>();
//		String id = mId.substring(6,mId.length());
		Comment comment = new Comment(uName,createTime,msg,uId,mId,isReply);
		int result = commentBiz.insertComment(comment);
		if(isReply == 1){
			commentBiz.insertRelation(comment.getcId(),cId,mId);
		}
		if(result > 0){
			map.put("commitResult","success");
		}else{
			map.put("commitResult","lose");
		}
		return map;
	}

	//删除评论
	@ResponseBody
	@RequestMapping("/deleteComment")
	public Object deleteComment(Integer cId){
		int result = commentBiz.deleteComment(cId);
		Map<String,String> map = new HashMap<>();
		if(result > 0){
			map.put("delResult","success");
		}else{
			map.put("delResult","lose");
		}
		return map;
	}

	//点赞
	@ResponseBody
	@RequestMapping("/clickZan")
	public Object clickZan(Integer cId,Integer uId,Integer isZan){
		int commentResult = commentBiz.updateLikeNum(cId,uId,isZan);
		int votelogResult = commentBiz.updateVotelog(cId,uId,isZan);
		Map<String,String> map = new HashMap<>();
		if(commentResult > 0 && votelogResult > 0){
			map.put("clickZan","success");
		}else{
			map.put("clickZan","lose");
		}
		return map;
	}

	//添加点赞关系
	@ResponseBody
	@RequestMapping("/insertVotelog")
	public Object insertVotelog(Integer cId,Integer uId,Integer isZan){
		Map<String,String> map = new HashMap<>();
		VoteLog voteLog = commentBiz.selectVoteLogById(cId,uId);
		if(isZan == 0 && voteLog == null){
			Date time = new Date();
			VoteLog vote = new VoteLog(uId,time,cId,isZan);
			int result = commentBiz.insertVotelog(vote);
			if(result > 0){
				map.put("addVoteResult","success");
			}else{
				map.put("addVoteResult","lose");
			}
		}
		return map;
	}


}
